﻿using System;
using System.Collections.Generic;
using System.Linq;
using Illusion;
using SEXY;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

namespace Studio
{
	// Token: 0x0200043A RID: 1082
	public class CharaList : MonoBehaviour
	{
		// Token: 0x1700029A RID: 666
		// (get) Token: 0x06001AD9 RID: 6873 RVA: 0x000AAF9C File Offset: 0x000A919C
		// (set) Token: 0x06001ADA RID: 6874 RVA: 0x000AAFA4 File Offset: 0x000A91A4
		public bool isInit { get; private set; }

		// Token: 0x06001ADB RID: 6875 RVA: 0x000AAFB0 File Offset: 0x000A91B0
		public void InitCharaList(bool _force = false)
		{
			if (this.isInit && !_force)
			{
				return;
			}
			this.charaFileSort.DeleteAllNode();
			if (this.sex == 1)
			{
				this.InitFemaleList();
			}
			else
			{
				this.InitMaleList();
			}
			int count = this.charaFileSort.cfiList.Count;
			for (int i = 0; i < count; i++)
			{
				CharaFileInfo info = this.charaFileSort.cfiList[i];
				info.index = i;
				GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.objectNode);
				if (!gameObject.activeSelf)
				{
					gameObject.SetActive(true);
				}
				gameObject.transform.SetParent(this.charaFileSort.root, false);
				info.gameSceneNode = gameObject.GetComponent<GameSceneNode>();
				info.button = gameObject.GetComponent<Button>();
				info.gameSceneNode.AddActionToButton(delegate
				{
					this.OnSelectChara(info.index);
				});
				info.gameSceneNode.text = info.name;
				info.gameSceneNode.listEnterAction.Add(delegate
				{
					this.LoadCharaImage(info.index);
				});
			}
			this.imageChara.color = Color.clear;
			this.charaFileSort.Sort(0, false);
			this.buttonLoad.interactable = false;
			this.buttonChange.interactable = false;
			this.isInit = true;
		}

		// Token: 0x06001ADC RID: 6876 RVA: 0x000AB138 File Offset: 0x000A9338
		private void OnSelectChara(int _idx)
		{
			if (this.charaFileSort.select == _idx)
			{
				return;
			}
			this.charaFileSort.select = _idx;
			this.buttonLoad.interactable = true;
			ObjectCtrlInfo ctrlInfo = Studio.GetCtrlInfo(Singleton<Studio>.Instance.treeNodeCtrl.selectNode);
			OCIChar ocichar = ctrlInfo as OCIChar;
			this.buttonChange.interactable = (ocichar != null && ocichar.oiCharInfo.sex == this.sex);
			this.isDelay = true;
			Observable.Timer(TimeSpan.FromMilliseconds(250.0)).Subscribe(delegate(long _)
			{
				this.isDelay = false;
			});
		}

		// Token: 0x06001ADD RID: 6877 RVA: 0x000AB1E4 File Offset: 0x000A93E4
		private void LoadCharaImage(int _idx)
		{
			if (this.isDelay)
			{
				return;
			}
			CharaFileInfo charaFileInfo = this.charaFileSort.cfiList[_idx];
			string file = charaFileInfo.file;
			this.imageChara.texture = PngAssist.LoadTexture(file);
			this.imageChara.color = Color.white;
			Resources.UnloadUnusedAssets();
			GC.Collect();
		}

		// Token: 0x06001ADE RID: 6878 RVA: 0x000AB244 File Offset: 0x000A9444
		public void OnSort(int _type)
		{
			this.charaFileSort.select = -1;
			this.buttonLoad.interactable = false;
			this.buttonChange.interactable = false;
			this.charaFileSort.Sort(_type);
		}

		// Token: 0x06001ADF RID: 6879 RVA: 0x000AB278 File Offset: 0x000A9478
		public void LoadCharaFemale()
		{
			Singleton<Studio>.Instance.AddFemale(this.charaFileSort.selectPath);
		}

		// Token: 0x06001AE0 RID: 6880 RVA: 0x000AB290 File Offset: 0x000A9490
		public void ChangeCharaFemale()
		{
			OCIChar[] array = (from v in Singleton<GuideObjectManager>.Instance.selectObjectKey
			select Studio.GetCtrlInfo(v) as OCIChar into v
			where v != null
			where v.oiCharInfo.sex == 1
			select v).ToArray<OCIChar>();
			foreach (OCIChar ocichar in array)
			{
				ocichar.ChangeChara(this.charaFileSort.selectPath);
			}
		}

		// Token: 0x06001AE1 RID: 6881 RVA: 0x000AB340 File Offset: 0x000A9540
		private void InitFemaleList()
		{
			List<string> list = new List<string>();
			string folder = UserData.Path + "chara/female";
			Utils.File.GetAllFiles(folder, "*.png", ref list);
			this.charaFileSort.cfiList.Clear();
			CharaInfo charaInfo = new CharaInfo();
			foreach (string text in list)
			{
				charaInfo.Load(text);
				this.charaFileSort.cfiList.Add(new CharaFileInfo(string.Empty, string.Empty)
				{
					file = text,
					name = charaInfo.name,
					time = charaInfo.time
				});
			}
		}

		// Token: 0x06001AE2 RID: 6882 RVA: 0x000AB418 File Offset: 0x000A9618
		public void LoadCharaMale()
		{
			Singleton<Studio>.Instance.AddMale(this.charaFileSort.selectPath);
		}

		// Token: 0x06001AE3 RID: 6883 RVA: 0x000AB430 File Offset: 0x000A9630
		public void ChangeCharaMale()
		{
			OCIChar[] array = (from v in Singleton<GuideObjectManager>.Instance.selectObjectKey
			select Studio.GetCtrlInfo(v) as OCIChar into v
			where v != null
			where v.oiCharInfo.sex == 0
			select v).ToArray<OCIChar>();
			foreach (OCIChar ocichar in array)
			{
				ocichar.ChangeChara(this.charaFileSort.selectPath);
			}
		}

		// Token: 0x06001AE4 RID: 6884 RVA: 0x000AB4E0 File Offset: 0x000A96E0
		private void InitMaleList()
		{
			List<string> list = new List<string>();
			string folder = UserData.Path + "chara/male";
			Utils.File.GetAllFiles(folder, "*.png", ref list);
			this.charaFileSort.cfiList.Clear();
			CharaInfo charaInfo = new CharaInfo();
			foreach (string text in list)
			{
				charaInfo.Load(text);
				this.charaFileSort.cfiList.Add(new CharaFileInfo(string.Empty, string.Empty)
				{
					file = text,
					name = charaInfo.name,
					time = charaInfo.time
				});
			}
		}

		// Token: 0x06001AE5 RID: 6885 RVA: 0x000AB5B8 File Offset: 0x000A97B8
		private void OnSelect(TreeNodeObject _node)
		{
			if (_node == null)
			{
				return;
			}
			if (!Singleton<Studio>.IsInstance())
			{
				return;
			}
			ObjectCtrlInfo objectCtrlInfo = null;
			if (!Singleton<Studio>.Instance.dicInfo.TryGetValue(_node, out objectCtrlInfo))
			{
				this.buttonChange.interactable = false;
				return;
			}
			if (objectCtrlInfo.kind != 0)
			{
				this.buttonChange.interactable = false;
				return;
			}
			OCIChar ocichar = objectCtrlInfo as OCIChar;
			if (ocichar == null || ocichar.oiCharInfo.sex != this.sex)
			{
				this.buttonChange.interactable = false;
				return;
			}
			if (this.charaFileSort.select != -1)
			{
				this.buttonChange.interactable = true;
			}
		}

		// Token: 0x06001AE6 RID: 6886 RVA: 0x000AB668 File Offset: 0x000A9868
		private void OnDelete(ObjectCtrlInfo _info)
		{
			if (_info == null)
			{
				return;
			}
			if (_info.kind != 0)
			{
				return;
			}
			OCIChar ocichar = _info as OCIChar;
			if (ocichar == null || ocichar.oiCharInfo.sex != this.sex)
			{
				return;
			}
			if (this.charaFileSort.select != -1)
			{
				this.buttonChange.interactable = false;
			}
		}

		// Token: 0x06001AE7 RID: 6887 RVA: 0x000AB6CC File Offset: 0x000A98CC
		private void Awake()
		{
			this.isInit = false;
			this.InitCharaList(false);
			TreeNodeCtrl treeNodeCtrl = Singleton<Studio>.Instance.treeNodeCtrl;
			treeNodeCtrl.onSelect = (Action<TreeNodeObject>)Delegate.Combine(treeNodeCtrl.onSelect, new Action<TreeNodeObject>(this.OnSelect));
			Studio instance = Singleton<Studio>.Instance;
			instance.onDelete = (Action<ObjectCtrlInfo>)Delegate.Combine(instance.onDelete, new Action<ObjectCtrlInfo>(this.OnDelete));
		}

		// Token: 0x04001D86 RID: 7558
		[SerializeField]
		private int sex = 1;

		// Token: 0x04001D87 RID: 7559
		[SerializeField]
		private GameObject objectNode;

		// Token: 0x04001D88 RID: 7560
		[SerializeField]
		private RawImage imageChara;

		// Token: 0x04001D89 RID: 7561
		[SerializeField]
		private CharaFileSort charaFileSort = new CharaFileSort();

		// Token: 0x04001D8A RID: 7562
		[SerializeField]
		private Button buttonLoad;

		// Token: 0x04001D8B RID: 7563
		[SerializeField]
		private Button buttonChange;

		// Token: 0x04001D8D RID: 7565
		private bool isDelay;
	}
}
